package com.leetcode.util;

import java.util.ArrayList;
import java.util.List;

public class LinkUtil {
    public static ListNode createEmptNode(){
        return new ListNode();
    }
    public static ListNode createHeadNode(Integer value){
        return new ListNode(value);
    }
    public static ListNode createNextNode(ListNode listNode,Integer value){
        listNode.next = new ListNode(value);
        ListNode emptNode = createEmptNode();
        emptNode = listNode.next;
        return emptNode;
    }
    public static Integer getValue(ListNode listNode){
        return listNode.val;
    }
    public static ListNode getNext(ListNode listNode){
        return listNode.next;
    }
    public static void setValue(ListNode listNode,Integer value){
        listNode.val = value;
    }
    public static void forEach(ListNode listNode,Boolean isOrder){
        ListNode tempNode = listNode;
        List<Integer> list = new ArrayList<>();
        list.add(tempNode.val);
        while (tempNode.hasNext()){
            tempNode = tempNode.next;
            list.add(tempNode.val);
        }
        if(isOrder){
            System.out.print("head:");
            for(Integer val : list){
                System.out.print(val + "->");
            }
        }else {
            System.out.print("tail:");
            for(Integer i = list.size()-1;i>=0;i--){
                System.out.print(list.get(i) + "->");
            }
        }
        System.out.println();
    }

}
